import BASE_URL from './config'
const request = (options) => {
	return new Promise((resolve, reject) => {
		uni.request({
			url: BASE_URL.fankongApp + options.url,
			method: options.method || 'GET',
			header: {
				'Authorization': uni.getStorageSync("fankongApp_key") || ""
			},
			responseType: options.responseType,
			data: options.data || {},
			success: (result) => {
				// console.log("接口打印", result);
				if (result.statusCode == 200) {
					if (result.data.code == 200) {
						resolve(result.data)
					} else if (result.data.code == 501) {
						resolve(result.data)
					} else if (result.data.code == 401) {
						uni.removeStorageSync('fankongApp_key');
						uni.redirectTo({
							url: '/pages/login/login'
						})
						reject(result.data)
					} else {
						resolve(result.data)
						//弹出异常提示
					}
				} else if (result.statusCode == 401) {
					// token 过期

					uni.removeStorageSync('fankongApp_key');
					uni.redirectTo({
						url: '/pages/login/login'
					})
					reject(result.data)
				} else if (result.statusCode == 555) {
					// 其他异常
					if (result.data.code == 405) {
						if (result.data.msg == '用户名或密码错误') {
							uni.showToast({
								position: 'top',
								icon: 'error',
								title: '用户名密码错误',
								duration: 2000,
							});
							uni.hideLoading();
						} else {
							uni.showToast({
								position: 'top',
								icon: 'error',
								title: result.data.msg,
								duration: 2000,
							});
							uni.hideLoading();
						}
					} else if (result.code == 500) {
						console.log(500);
						uni.showToast({
							position: 'top',
							icon: 'error',
							title: '服务异常',
							duration: 2000,
						});
						uni.hideLoading();
					}
				} else {
					console.log(result);
					uni.showToast({
						position: 'top',
						icon: 'loading',
						title: result.data.message,
						duration: 2000,
					});
				}
			},
			fail: (err) => {
				reject(err)
			}
		})
	})
}
export default request